package com.haois.ws.web.module.rbac.service;

import com.haois.ws.web.module.rbac.daoman.Manager;
import com.haois.ws.common.util.Identities;
import com.haois.ws.common.util.MD5Coder;
import com.haois.ws.web.module.rbac.dao.mapper.ManagerMapper;
import com.haois.ws.common.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by haol on 2017/3/14.
 */
@Service
public class ManagerService {

    @Autowired
    private ManagerMapper managerMapper;


    public Manager getByAccount(String account) {
        return managerMapper.getByAccount(account);
    }

    public void updateLogin(String id, Date nowTime) {
        Manager manager = new Manager();
        manager.setId(id);
        manager.setLoginTime(nowTime);
        managerMapper.updateLogin(manager);
    }

    public void add(Manager manager) {
        String id = Identities.uuid();
        manager.setId(id);
        manager.setCreateTime(DateUtil.getYMdHms());
        String MD5 = MD5Coder.encodeCombine(manager.getAccount(), manager.getPassword());
        manager.setPassword(MD5);
        managerMapper.add(manager);
    }

    public boolean modifyPwd(String id, String oldPwd, String newPwd) {
        return true;
    }

    public boolean comparePwd(String inputOldPwd, Manager manager) {
        inputOldPwd = MD5Coder.encodeCombine(manager.getAccount(), inputOldPwd);
        String oldPwd = manager.getPassword();
        if (inputOldPwd.equals(oldPwd)) {
            return true;
        } else {
            return false;
        }
    }

    public void update(Manager manager) {
        managerMapper.update(manager);
        managerMapper.updateTime(manager.getId(), DateUtil.getYMdHms());
    }

    public void updatePicture(String mid, String pictureUrl) {
        managerMapper.updatePicture(mid, pictureUrl);
        managerMapper.updateTime(mid, DateUtil.getYMdHm());
    }
}
